/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  UserInfoCheckActivity.java   
 * @Package com.jiyi.activity.leader   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-9-27 上午11:10:54   
 * @version V1.0     
 */
package com.jiyi.activity.datainput;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.adapter.DividerItemDecoration;
import com.jiyi.adapter.recyclerview.CommonAdapter;
import com.jiyi.adapter.recyclerview.base.ViewHolder;
import com.jiyi.adapter.recyclerview.wrapper.EmptyWrapper;
import com.jiyi.bean.datainput.AssessUserBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: UserInfoCheckActivity
 * @Description:TODO(用户信息审核列表)
 * @author: HYM
 * @date: 2017-9-27 上午11:10:54
 * 
 */
public class DIUserInfoCheckActivity extends ActivityBase implements
		OnRefreshListener {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.mainList)
	RecyclerView mListView;
	@BindView(R.id.refreshLayout)
	SwipeRefreshLayout refreshLayout;
	EmptyWrapper adapter;
	private LayoutInflater inflater;

	private List<AssessUserBean> mCheckUserInfoBeans = new ArrayList<AssessUserBean>();
	//进度条
	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo_check);
		ButterKnife.bind(this);
		init();
	}

	private void init() {
		inflater = LayoutInflater.from(this);
		setTop();
		setAdapter();
		getData();
	}

	private void setTop() {
		tvTitle.setText("用户信息审核");
		tvSearch.setVisibility(View.GONE);
	}

	private void setAdapter() {
		// 设置refreshLayout的监听
		refreshLayout.setColorSchemeColors(Color.RED, Color.YELLOW, Color.BLUE,
				Color.GREEN);
		refreshLayout.setOnRefreshListener(this);

		mListView.setLayoutManager(new LinearLayoutManager(this));
		mListView.addItemDecoration(new DividerItemDecoration(this,
				DividerItemDecoration.VERTICAL_LIST, getResources()
						.getDrawable(R.drawable.drvice_10)));

		CommonAdapter mCommonAdapter = new CommonAdapter<AssessUserBean>(
				this, R.layout.item_di_userinfo_check, mCheckUserInfoBeans) {
			@Override
			protected void convert(ViewHolder holder,
					final AssessUserBean t, final int position) {
				holder.setText(R.id.tvCheckUser, t.getUser_name());
				holder.setText(R.id.tvRole, t.getAssess() == 1? "调查主管":"调查员");
				holder.setOnClickListener(R.id.tvAgree, new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						agree(t.getId(),position);
					}
				});
				
				holder.setOnClickListener(R.id.tvDisAgree, new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						disAgree(t.getId(),position);
					}
				});
				
				switch (t.getStatus()) {
				case 2:
					//不同意
					holder.setVisible(R.id.tvAgree, false);
					holder.setText(R.id.tvDisAgree, "已拒绝");
					holder.setVisible(R.id.tvDisAgree, true);
					holder.setEnable(R.id.tvDisAgree, false);
					break;
				case 1:
					//同意
					holder.setVisible(R.id.tvDisAgree, false);
					holder.setText(R.id.tvAgree, "已同意");
					holder.setVisible(R.id.tvAgree, true);
					holder.setEnable(R.id.tvAgree, false);
					break;
				default:
					//待审核
					holder.setEnable(R.id.tvAgree, true);
					holder.setEnable(R.id.tvDisAgree, true);
					holder.setText(R.id.tvAgree, "同意");
					holder.setText(R.id.tvDisAgree, "拒绝");
					holder.setVisible(R.id.tvDisAgree, true);
					holder.setVisible(R.id.tvAgree, true);
					break;
				}
			}
		};

		adapter = new EmptyWrapper(mCommonAdapter);

		View emptyView = inflater.inflate(R.layout.empty_view_text_and_image,
				mListView, false);
		ImageView ivEmptyTip = (ImageView) emptyView
				.findViewById(R.id.ivEmptyTip);
		TextView tvEmptyTip = (TextView) emptyView
				.findViewById(R.id.tvEmptyTip);
		ivEmptyTip.setImageResource(R.mipmap.tip_empty_huodong);
		tvEmptyTip.setText(R.string.NO_DATA);
		adapter.setEmptyView(emptyView);

		mListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onRefresh() {
		getData();
	}

	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		VolleyRequest.StringRequestPost(this,
				WebApi.DATA_INPUT_SQQX_LIST, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								List<AssessUserBean> temp = new Gson()
										.fromJson(
												data,
												new TypeToken<List<AssessUserBean>>() {
												}.getType());
								mCheckUserInfoBeans.clear();
								mCheckUserInfoBeans.addAll(temp);
								adapter.notifyDataSetChanged();
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							refreshLayout.setRefreshing(false);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						refreshLayout.setRefreshing(false);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}
	
	private void agree(String user_id,final int position) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", user_id);
		VolleyRequest.StringRequestPost(this,
				WebApi.DATA_INPUT_SQQX_AGREE, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								mCheckUserInfoBeans.get(position).setStatus(1);
								adapter.notifyItemChanged(position);
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}
	
	private void disAgree(String user_id,final int position) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", user_id);
		VolleyRequest.StringRequestPost(this,
				WebApi.DATA_INPUT_SQQX_DISAGREE, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								mCheckUserInfoBeans.get(position).setStatus(2);
								adapter.notifyItemChanged(position);
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}

	@OnClick({ R.id.tvBack })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		default:
			break;
		}
	}
}
